{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "443e8127",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-29T09:05:44.148340Z",
     "start_time": "2023-10-29T09:05:40.342315Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to distilbert-base-uncased-finetuned-sst-2-english and revision af0f99b (https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'label': 'NEGATIVE', 'score': 0.9991129040718079}\n",
      "{'label': 'POSITIVE', 'score': 0.9998656511306763}\n"
     ]
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "#文本分类\n",
    "classifier = pipeline(\"sentiment-analysis\")\n",
    "\n",
    "result = classifier(\"I hate you\")[0]\n",
    "print(result)\n",
    "\n",
    "result = classifier(\"I love you\")[0]\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e2fca99d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-29T09:05:47.602600Z",
     "start_time": "2023-10-29T09:05:46.149216Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to distilbert-base-cased-distilled-squad and revision 626af31 (https://huggingface.co/distilbert-base-cased-distilled-squad).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'score': 0.6177281737327576, 'start': 34, 'end': 95, 'answer': 'the task of extracting an answer from a text given a question'}\n",
      "{'score': 0.5152309536933899, 'start': 148, 'end': 161, 'answer': 'SQuAD dataset'}\n"
     ]
    }
   ],
   "source": [
    "#阅读理解\n",
    "question_answerer = pipeline(\"question-answering\")\n",
    "\n",
    "context = r\"\"\"\n",
    "Extractive Question Answering is the task of extracting an answer from a text given a question. An example of a \n",
    "question answering dataset is the SQuAD dataset, which is entirely based on that task. If you would like to fine-tune \n",
    "a model on a SQuAD task, you may leverage the examples/pytorch/question-answering/run_squad.py script.\n",
    "\"\"\"\n",
    "\n",
    "result = question_answerer(question=\"What is extractive question answering?\",\n",
    "                           context=context)\n",
    "print(result)\n",
    "\n",
    "result = question_answerer(\n",
    "    question=\"What is a good example of a question answering dataset?\",\n",
    "    context=context)\n",
    "\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b2d0fa4f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-29T09:05:52.053739Z",
     "start_time": "2023-10-29T09:05:50.163857Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to distilroberta-base and revision ec58a5b (https://huggingface.co/distilroberta-base).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Some weights of the model checkpoint at distilroberta-base were not used when initializing RobertaForMaskedLM: ['roberta.pooler.dense.weight', 'roberta.pooler.dense.bias']\n",
      "- This IS expected if you are initializing RobertaForMaskedLM from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing RobertaForMaskedLM from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'score': 0.17927537858486176,\n",
       "  'token': 3944,\n",
       "  'token_str': ' tool',\n",
       "  'sequence': 'HuggingFace is creating a tool that the community uses to solve NLP tasks.'},\n",
       " {'score': 0.11349591612815857,\n",
       "  'token': 7208,\n",
       "  'token_str': ' framework',\n",
       "  'sequence': 'HuggingFace is creating a framework that the community uses to solve NLP tasks.'},\n",
       " {'score': 0.05243527889251709,\n",
       "  'token': 5560,\n",
       "  'token_str': ' library',\n",
       "  'sequence': 'HuggingFace is creating a library that the community uses to solve NLP tasks.'},\n",
       " {'score': 0.0349353589117527,\n",
       "  'token': 8503,\n",
       "  'token_str': ' database',\n",
       "  'sequence': 'HuggingFace is creating a database that the community uses to solve NLP tasks.'},\n",
       " {'score': 0.02860235422849655,\n",
       "  'token': 17715,\n",
       "  'token_str': ' prototype',\n",
       "  'sequence': 'HuggingFace is creating a prototype that the community uses to solve NLP tasks.'}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#完形填空\n",
    "unmasker = pipeline(\"fill-mask\")\n",
    "\n",
    "from pprint import pprint\n",
    "\n",
    "sentence = 'HuggingFace is creating a <mask> that the community uses to solve NLP tasks.'\n",
    "\n",
    "unmasker(sentence)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "bbded684",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-29T09:05:58.686129Z",
     "start_time": "2023-10-29T09:05:54.859981Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No model was supplied, defaulted to gpt2 and revision 6c0e608 (https://huggingface.co/gpt2).\n",
      "Using a pipeline without specifying a model name and revision in production is not recommended.\n",
      "Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[{'generated_text': 'As far as I am concerned, I will be the first to admit that I am not a fan of the idea of a \"free market.\" I think that the idea of a free market is a bit of a stretch. I think that the idea'}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from transformers import pipeline\n",
    "\n",
    "#文本生成\n",
    "text_generator = pipeline(\"text-generation\")\n",
    "\n",
    "text_generator(\"As far as I am concerned, I will\",\n",
    "               max_length=50,\n",
    "               do_sample=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python(d2l)",
   "language": "python",
   "name": "d2l"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
